package com.itheima.reggie.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.mapper.OrdersMapper;
import com.itheima.reggie.mapper.UserMapper;
import com.itheima.reggie.service.*;
import com.itheima.reggie.util.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


@Service


public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {


    @Autowired
    OrderDetailService orderDetailService;
    @Autowired
    ShoppingCartService shoppingCartService;
    @Autowired
    UserService userService;
    @Autowired
    AddressBookService addressBookService;

    @Override
    public void submit(Orders orders) {
        //获取用户购物车数据
        Long addressBookId = orders.getAddressBookId();
        Integer payMethod = orders.getPayMethod();
        Long userId = BaseContext.getId();
        BigDecimal total = new BigDecimal(0);
        //订单id
        long id = IdWorker.getId();


        LambdaQueryWrapper<ShoppingCart> queryWrapper = Wrappers.lambdaQuery(ShoppingCart.class).eq(ShoppingCart::getUserId, userId);

        List<ShoppingCart> ShoppingCartList = shoppingCartService.list(queryWrapper);


        //生成订单明细
        List<OrderDetail> orderDetails = new ArrayList<>();

        for (ShoppingCart shoppingCart : ShoppingCartList) {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderId(id);
            orderDetail.setName(shoppingCart.getName());
            orderDetail.setDishId(shoppingCart.getDishId());
            orderDetail.setSetmealId(shoppingCart.getSetmealId());
            //orderDetail.setDishFlavor();
            orderDetail.setNumber(shoppingCart.getNumber());
            orderDetail.setAmount(shoppingCart.getAmount());
            orderDetail.setImage(shoppingCart.getImage());

            //todo
            total = total.add(shoppingCart.getAmount().multiply(new BigDecimal((shoppingCart.getNumber()))));
            orderDetails.add(orderDetail);
        }
        orderDetailService.saveBatch(orderDetails);


        //生成订单
        Orders newOrders = new Orders();

        newOrders.setId(id);
        newOrders.setNumber(String.valueOf(id));
        newOrders.setStatus(2);
        newOrders.setUserId(userId);
        newOrders.setAddressBookId(addressBookId);
        newOrders.setOrderTime(LocalDateTime.now());
        newOrders.setCheckoutTime(LocalDateTime.now());
        newOrders.setPayMethod(payMethod);
        newOrders.setAmount(total);
        newOrders.setRemark(orders.getRemark());
        newOrders.setUserName(userService.getById(userId).getName());
        newOrders.setPhone(userService.getById(userId).getPhone());
        newOrders.setAddress(addressBookService.getById(addressBookId).getDetail());
        newOrders.setConsignee(addressBookService.getById(addressBookId).getConsignee());
        this.save(newOrders);

        //删除已结算的购物车数据
        shoppingCartService.remove(queryWrapper);
    }
}
